/*
 * @lc app=leetcode.cn id=443 lang=cpp
 *
 * [443] 压缩字符串
 */

// @lc code=start
class Solution
{
public:
  int compress(vector<char>& chars)
  {
    int n = chars.size(), len = 0;
    for (int i = 0; i < n;) {
      chars[len++] = chars[i];
      int j = i + 1;
      while (j < n && chars[j] == chars[i])
        ++j;
      if (j - i > 1)
        for (char& c : to_string(j - i))
          chars[len++] = c;
      i = j;
    }
    return len;
  }
};
// @lc code=end
